package yiran.depositcumulative.command;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import yiran.depositcumulative.DepositCumulative;
import yiran.depositcumulative.data.DatabaseService;
import yiran.depositcumulative.lang.LangHelper;

public class CumulativeCommand implements CommandExecutor {
    private final DatabaseService databaseService;
    private final DepositCumulative plugin;

    public CumulativeCommand(DatabaseService databaseService, DepositCumulative plugin) {
        this.databaseService = databaseService;
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        // 帮助指令
        if (args.length == 0 || "help".equalsIgnoreCase(args[0])) {
            sendHelp(sender);
            return true;
        }

        // 权限检查（查看他人累计）
        if (!"all".equalsIgnoreCase(args[0]) && !"top".equalsIgnoreCase(args[0]) && !sender.hasPermission("deposit.cumulative.others")) {
            sender.sendMessage(LangHelper.getString("command.no_permission"));
            return true;
        }

        // 查询自己
        if ("self".equalsIgnoreCase(args[0])) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                int total = databaseService.getTotalDeposit(player.getName());
                String message = LangHelper.getString("command.cumulative.self", total);
                sender.sendMessage(message);
                return true;
            }
            sender.sendMessage(LangHelper.getString("command.only_player"));
            return true;
        }

        // 打开排行榜
        if ("top".equalsIgnoreCase(args[0])) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(LangHelper.getString("command.only_player"));
                return true;
            }

            if (!sender.hasPermission("deposit.cumulative.top")) {
                sender.sendMessage(LangHelper.getString("command.no_permission"));
                return true;
            }

            Player player = (Player) sender;
            plugin.getGuiListener().openTopGui(player);
            return true;
        }

        // 查询全服总额
        if ("all".equalsIgnoreCase(args[0]) && sender.hasPermission("deposit.cumulative.all")) {
            int total = databaseService.getTotalDepositAll();
            sender.sendMessage(LangHelper.getString("command.cumulative.all", total));
            return true;
        }

        // 查询指定玩家
        String playerName = args[0];
        int total = databaseService.getTotalDeposit(playerName);

        // 验证玩家是否存在
        OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerName);
        if (!offlinePlayer.hasPlayedBefore()) {
            sender.sendMessage(LangHelper.getString("command.player_not_found", playerName));
            return true;
        }

        if (sender instanceof Player && sender.getName().equals(playerName)) {
            // 玩家查询自己的累计
            sender.sendMessage(LangHelper.getString("command.cumulative.self", total));
        } else {
            // 查询他人累计
            sender.sendMessage(LangHelper.getString("command.cumulative.other", playerName, total));
        }

        return true;
    }

    private void sendHelp(CommandSender sender) {
        LangHelper.getStringList("command.help").forEach(sender::sendMessage); // 修复键名
    }
}

